package com.example.springboottest.controller;


import com.example.springboottest.entity.Student;
import com.example.springboottest.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentHandler {
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Student> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return studentRepository.findAll(pageable);
    }

    @PostMapping("/save")
    public String save(@RequestBody Student student){
        Student result = studentRepository.save(student);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @GetMapping("/findById/{s_id}")
    public Student findById(@PathVariable("s_id") Integer id){

        return studentRepository.findById(id).get();
    }

    @DeleteMapping("/deleteById/{s_id}")
    public String  deleteById(@PathVariable("s_id") Integer id){
        studentRepository.deleteById(id);
        return "success";
    }
}
